package io.gitee.crud.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.gitee.crud.converter.DemoUserConverter;
import io.gitee.crud.model.dto.DemoUserQueryDTO;
import io.gitee.crud.model.dto.DemoUserSaveDTO;
import io.gitee.crud.model.dto.DemoUserUpdateDTO;
import io.gitee.crud.model.entity.DemoUserEntity;
import io.gitee.crud.model.vo.DemoUserVO;
import io.gitee.crud.service.DemoUserService;
import io.gitee.crud.common.Result;
import io.gitee.crud.common.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RequestMapping("/api/demo/user")
@RestController
public class DemoUserController {

    @Autowired
    private DemoUserService demoUserService;

    @PostMapping("/save")
    public Result<Void> save(@Valid @RequestBody DemoUserSaveDTO dto) {
        demoUserService.save(DemoUserConverter.toEntity(dto));
        return Result.success();
    }

    @PostMapping("/delete/{userIds}")
    public Result<Void> delete(@PathVariable("userIds") List<Long> userIds) {
        demoUserService.delete(userIds);
        return Result.success();
    }

    @PostMapping("/update")
    public Result<Void> update(@Valid @RequestBody DemoUserUpdateDTO dto) {
        demoUserService.update(DemoUserConverter.toEntity(dto));
        return Result.success();
    }

    @GetMapping("/list")
    public Result<PageResult<DemoUserVO>> list(@Valid DemoUserQueryDTO dto) {
        IPage<DemoUserEntity> list = demoUserService.list(dto.getCreateStart(), dto.getCreateEnd());
        return Result.success(DemoUserConverter.toPageResult(list));
    }
}
